package cn.haiway.drools.web;

import cn.haiway.drools.build.BuildService;
import cn.haiway.drools.pojo.Project;
import cn.haiway.drools.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.Date;

@RestController
@RequestMapping("/api/projects")
@Slf4j
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private BuildService buildService;

    @PostMapping
    public ResponseEntity<Project> createProject(@Valid @RequestBody Project projectDTO) throws URISyntaxException {
        log.debug("REST request to save Project : {}", projectDTO);

        projectDTO.setCreateTime(new Date());
        projectDTO.setCreateBy("System");
        Project result = projectService.save(projectDTO);
        return ResponseEntity.ok().body(result);
    }

    @PostMapping("/{id}")
    public void publishProjectAfterBuild(@PathVariable String id) {
        log.debug("REST request to get Project : {}", id);
        buildService.build(id);
    }

}
